package com.tz.colawallet.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseActivity;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.biz.RegisterBiz;
import com.tz.colawallet.ui.component.DaggerRegisterComponent;
import com.tz.colawallet.ui.contract.RegisterContract;
import com.tz.colawallet.ui.module.RegisterModule;
import com.tz.colawallet.ui.presenter.RegisterPresenter;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

public class RegisterActivity extends BaseActivity implements RegisterContract.View {
    private static final int HANDLER_TIMER = 1;
    @Inject
    RegisterPresenter presenter;
    @Inject
    RegisterBiz biz;

    EditText phone;
    EditText code;
    Button getCode;
    EditText password;
    Button register;
    TextView registerToLogin;
    TextView registerTitle;
    TextView registerToLoginText;


    private Timer mTimer;
    private MyTimerTask myTimerTask;
    private int countdown = 60;

    @Override
    protected void setup() {
        super.setup();
        DaggerRegisterComponent.builder().registerModule(new RegisterModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void init() {
        phone = findViewById(R.id.registerPhone);
        code = findViewById(R.id.code);
        getCode = findViewById(R.id.getCode);
        password = findViewById(R.id.password);
        register = findViewById(R.id.register);
        registerToLogin = findViewById(R.id.register_to_login);
        registerTitle = findViewById(R.id.register_title);
        registerToLoginText = findViewById(R.id.register_to_login_text);


        if (getIntent().getFlags() == Key.FORGET) {
            register.setText("确认");
            registerTitle.setText("忘记密码");
            registerToLoginText.setText("想起账号？去");
        } else {
            register.setText("注册");
            registerTitle.setText("注册");
            registerToLoginText.setText("已有账号？去");

        }

        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (countdown != 60) {
                    showToast("请在倒计时结束后获取验证码");
                    return;
                }
                if (phone.getText().toString().isEmpty()) {
                    showToast("请输入手机号");
                    return;
                }
                if (phone.getText().toString().length() != 11) {
                    showToast("请输入正确手机号");
                    return;
                }
                presenter.sendSms(phone.getText().toString());
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone.getText().toString().isEmpty()) {
                    showToast("请输入手机号");
                    return;
                }
                if (phone.getText().toString().length() != 11) {
                    showToast("请输入正确手机号");
                    return;
                }
                if (code.getText().toString().isEmpty()) {
                    showToast("请输入短信验证码");
                    return;
                }
                if (password.getText().toString().isEmpty()) {
                    showToast("请输入新密码");
                    return;
                }
                if (password.getText().toString().length() < 6) {
                    showToast("请输入新密码（最少6位）");
                    return;
                }

                if (getIntent().getFlags() == Key.FORGET) {
                    presenter.forget(phone.getText().toString(), password.getText().toString(), code.getText().toString());
                } else {
                    presenter.register(phone.getText().toString(), password.getText().toString(), code.getText().toString());
                }
            }
        });

        registerToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public Context curContext() {
        return this;
    }

    @Override
    public void sendSmsSuccess() {
        showToast("获取验证码成功");
        if (myTimerTask == null && mTimer == null) {
            mTimer = new Timer();
            myTimerTask = new MyTimerTask();
            //延时0ms后执行，1000ms执行一次
            mTimer.schedule(myTimerTask, 0, 1000);
        } else {
            mTimer.schedule(myTimerTask, 0, 1000);
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public void registerSuccess() {
        if (getIntent().getFlags() == Key.FORGET) {
            showToast("重置密码成功请重新登陆");
        } else {
            showToast("注册成功请重新登陆");
        }
        finish();
    }

    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            Message message = new Message();
            message.what = HANDLER_TIMER;
            handler.sendMessage(message);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_TIMER:
                    countdown--;
                    getCode.setText(countdown + "s");
                    getCode.setBackgroundColor(getResources().getColor(R.color.gray_btn));
                    if (0 == countdown) {
                        if (myTimerTask != null && mTimer != null) {
                            myTimerTask.cancel(); //退出计时器
                            myTimerTask = null;
                            mTimer.cancel();
                            mTimer = null;
                            getCode.setText("获取验证码");
                            getCode.setBackgroundResource(R.mipmap.bg_button);
                            countdown = 60;
                        }
                    }
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myTimerTask != null && mTimer != null) {
            myTimerTask.cancel(); //退出计时器
            myTimerTask = null;
            mTimer.cancel();
            mTimer = null;
        }
    }
}
